package com.furnitur.modules.portal.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.furnitur.common.api.Result;
import com.furnitur.core.service.FileService;
import com.furnitur.modules.portal.dto.review.ReviewQueryDTO;
import com.furnitur.modules.portal.dto.review.ReviewSubmitDTO;
import com.furnitur.modules.portal.service.PortalReviewService;
import com.furnitur.modules.portal.vo.review.ReviewStatsVO;
import com.furnitur.modules.portal.vo.review.ReviewVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@Slf4j
@RestController
@RequestMapping("/api/portal/reviews")
public class PortalReviewController {

    @Autowired
    private PortalReviewService reviewService;
    @Autowired
    private FileService fileService;

    /**
     * 获取评价列表
     */
    @GetMapping("/{furnitureId}")
    public Result<Page<ReviewVO>> getReviews(
            @PathVariable Long furnitureId,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(defaultValue = "all") String filterType,
            @RequestParam(defaultValue = "time") String sortType) {
        
        ReviewQueryDTO queryDTO = new ReviewQueryDTO();
        queryDTO.setFurnitureId(furnitureId);
        queryDTO.setPageNum(pageNum);
        queryDTO.setPageSize(pageSize);
        queryDTO.setFilterType(filterType);
        queryDTO.setSortType(sortType);
        
        return Result.success(reviewService.getReviewPage(queryDTO));
    }

    /**
     * 获取评价统计信息
     */
    @GetMapping("/stats/{furnitureId}")
    public Result<ReviewStatsVO> getReviewStats(@PathVariable Long furnitureId) {
        return Result.success(reviewService.getReviewStats(furnitureId));
    }

    /**
     * 提交评价
     */
    @PostMapping
    public Result<Void> submitReview(@Validated @RequestBody ReviewSubmitDTO submitDTO) {
        reviewService.submitReview(submitDTO);
        return Result.success();
    }

    /**
     * 点赞/取消点赞评价
     */
    @PostMapping("/like/{reviewId}")
    public Result<Void> toggleLike(@PathVariable Long reviewId) {
        reviewService.toggleReviewLike(reviewId);
        return Result.success();
    }

    /**
     * 上传评价图片
     */
    @PostMapping("/upload")
    public Result<String> uploadImage(@RequestParam("file") MultipartFile file) {
        try {
            // 1. 检查文件类型
            String contentType = file.getContentType();
            if (contentType == null || !contentType.startsWith("image/")) {
                return Result.failed("只能上传图片文件");
            }

            // 2. 检查文件大小 (2MB)
            if (file.getSize() > 2 * 1024 * 1024) {
                return Result.failed("图片大小不能超过2MB");
            }

            // 3. 使用 FileService 上传图片
            String fileUrl = fileService.uploadImage(file);

            // 4. 返回访问URL
            return Result.success(fileUrl);
        } catch (Exception e) {
            log.error("Failed to upload review image", e);
            return Result.failed("图片上传失败");
        }
    }
} 